﻿using System;
using System.Management;
using ClusterAware;
using ClusterAwareAdmin.Properties;
using ClusterAwareProperties;

namespace ClusterAwareAdmin
{
    /// <summary>
    /// Admin Network Methods Class
    /// </summary>
    public class NetworkAdmin: NetworkMethods
    {
        #region private properties

        private readonly ClusterManagerAdmin _managerclass;

        #endregion

        #region ctor

        /// <summary>
        /// Create Class GroupMethodsAdmin
        /// </summary>
        /// <param name="managerclass">Instance of ClusterManager class</param>
        public NetworkAdmin(ClusterManagerAdmin managerclass)
            : base(managerclass.ManagerInstance)
        {
            _managerclass = managerclass;
        }

        #endregion

        #region public Methods

        /// <summary>
        /// Rename a Network
        /// </summary>
        /// <param name="networkName">Network name</param>
        /// <param name="newNetworkName">New network name</param>
        public void Rename(string networkName, string newNetworkName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(networkName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "networkName"));
            }
            if (!_managerclass.Networks.ExistNetwork(networkName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Network", networkName));
            }
            if (string.IsNullOrEmpty(newNetworkName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "newNetworkName"));
            }
            if (_managerclass.Networks.ExistNetwork(newNetworkName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_AlreadyExist, "Network", newNetworkName));
            }
            try
            {
                var net = _managerclass.Networks.GetNetwork(networkName);
                using (var inParams = net.Instance.GetMethodParameters("Rename"))
                {
                    inParams["newName"] = newNetworkName;
                    net.Instance.InvokeMethod("Rename", inParams, null);
                    if (_managerclass.CacheInstances)
                    {
                        _managerclass.NetworkInstances.Remove(networkName);
                        _managerclass.Refresh(RefreshOption.Networks, newNetworkName);
                    }
                }
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Networks.Rename", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Networks.Rename", ex.Message), ex);
            }
        }

        #endregion


    }
}
